package org.maggioni.springsandbox.rehearsals.e06jdbc.e01basic;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.maggioni.springsandbox.rehearsals.e06jdbc.e01basic.dao.JdbcDaoImpl;
import org.maggioni.springsandbox.rehearsals.e06jdbc.e01basic.model.Job;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(Main.class.getName().replace(".","/")+"-applicationContext.xml");
		JdbcDaoImpl jdbcDaoImpl = applicationContext.getBean("jdbcDaoImpl",JdbcDaoImpl.class);
		String input = readString("Would you like to insert a job [yes/no]? ");
		if ( "yes".equalsIgnoreCase(input) ) {
			Job job = new Job();
			job.setJobId( readString("Job id (String): ") );
			job.setJobTitle( readString("Job Title (String): ") );
			job.setMinSalary( Integer.parseInt(readString("Job min salary (int): ")) );
			job.setMaxSalary( Integer.parseInt(readString("Job max salary (int): ")) );
			jdbcDaoImpl.insertJob(job);
		}
		System.out.println("Number of jobs: "+jdbcDaoImpl.getJobCount());
		System.out.println("Job title of AD_VP: "+jdbcDaoImpl.getJobTitle("AD_VP"));
		System.out.println("Job bean of AD_VP: "+jdbcDaoImpl.getJob("AD_VP"));
		System.out.println("Entire job table: "+jdbcDaoImpl.getAllJobs());
		System.out.println("Job with salary between 5000 and 20000: "+jdbcDaoImpl.getAllJobsBetweenSalary(5000, 20000));
	}


	private static String readString(String prompt) {
		System.out.print(prompt);

	      //  open up standard input
	      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	      String ret = null;

	      //  read the username from the command-line; need to use try/catch with the
	      //  readLine() method
	      try {
	         ret = br.readLine();
	      } catch (IOException ioe) {
	         System.out.println("IO error trying to read your name!");
	         System.exit(1);
	      }
	      return ret;
	}

}
